Skip to content

fix(api-handler): ensure deterministic request serialization - #252

Open
Tushar15769 wants to merge 2 commits into
metacall:masterfrom
Tushar15769:fix/deterministic-api-request-serialization-clean
Open

Tushar15769 wants to merge 2 commits into
metacall:masterfrom
Tushar15769:fix/deterministic-api-request-serialization-clean

Conversation

@Tushar15769

Copy link
Copy Markdown
Contributor

What I Did

This fixes Issue #251, where identical API requests could produce different JSON strings because the query, params, and headers maps use Rust's HashMap, which does not guarantee iteration order.

Cause

ApiRequest stores headers, params, and query as HashMap<String, String>.

When serde_json::to_string(&request) serializes these maps, their keys can appear in different orders even when the request data is identical.

This causes different JSON strings for equivalent requests, potentially affecting handlers that rely on request hashing, caching, or idempotency.

Changes

  • Added a custom serialize_sorted_map helper that uses BTreeMap to ensure consistent key ordering during serialization.
  • Applied the custom serializer to headers, params, and query.
  • Preserved the existing HashMap field types and deserialization behavior.
  • Added seven regression tests covering different insertion orders, repeated serialization, combined request maps, single-key requests, and empty maps.

Tests

  • Formatting check passed.
  • 40 unit tests and 36 documentation tests passed across the available workspace crates.
  • Added seven regression tests for deterministic request serialization.

Fixes #251

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: multi-key request maps make API request JSON nondeterministic

1 participant